//
//  DoubleFaceButton.m
//  MGameTribe
//
//  Created by 杨敏 on 13-6-27.
//  Copyright (c) 2013年 YangMin. All rights reserved.
//

#import "DoubleFaceButton.h"
#import "ConstantHeader.h"
#import "Tool.h"
@interface DoubleFaceButton()
{
    int currentIndex;
}
@end

@implementation DoubleFaceButton

-(void)setFaceOne_norm:(UIImage *)faceOne_norm{
    if (_faceOne_norm!=faceOne_norm) {
        _faceOne_norm =faceOne_norm;
    }
}

-(void)setFaceOne_high:(UIImage *)faceOne_high{
    if (_faceOne_high!=faceOne_high) {
        _faceOne_high =faceOne_high;
    }
    
}

-(void)setFaceTwo_norm:(UIImage *)faceTwo_norm{
    if (_faceTwo_norm!=faceTwo_norm) {
        _faceTwo_norm =faceTwo_norm;
    }
    
}

-(void)setFaceTwo_high:(UIImage *)faceTwo_high{
    if (_faceTwo_high!=faceTwo_high) {
        _faceTwo_high =faceTwo_high;
    }
}

-(void)showBacgroundImageWith:(int)index{
    [self showBacgroundImageWith:index clearImage:YES];
}

-(void)showBacgroundImageWith:(int)index clearImage:(BOOL)clearImage{
    currentIndex=index;
    
    if (clearImage) {
        [self setImage:nil forState:UIControlStateNormal];
        [self setImage:nil forState:UIControlStateHighlighted];
    }
    
    if (index==0) {
        [self setBackgroundImage:_faceOne_norm forState:UIControlStateNormal];
        [self setBackgroundImage:_faceOne_high forState:UIControlStateHighlighted];
    }
    else if(index==1){
        [self setBackgroundImage:_faceTwo_norm forState:UIControlStateNormal];
        [self setBackgroundImage:_faceTwo_high forState:UIControlStateHighlighted];
    }
    
    if ([_delegate respondsToSelector:@selector(doubleFaceButtonBacgroundImageDidChanged)]) {
        [_delegate doubleFaceButtonBacgroundImageDidChanged];
    }
    else{
        if ([_delegate isKindOfClass:[UIView class]]) {
            [(UIView *)_delegate setNeedsLayout];
        }
    }
    
}
-(void)showImageWith:(int)index{
    [self showImageWith:index clearBackgroundImage:YES];
}


-(void)showImageWith:(int)index clearBackgroundImage:(BOOL)clearBackgroundImage{

    currentIndex=index;
    if (clearBackgroundImage) {
        [self setBackgroundImage:nil forState:UIControlStateNormal];
        [self setBackgroundImage:nil forState:UIControlStateHighlighted];
    }

    if (index==0) {
        [self setImage:_faceOne_norm forState:UIControlStateNormal];
        [self setImage:_faceOne_high forState:UIControlStateHighlighted];
    }
    else if(index==1){
        [self setImage:_faceTwo_norm forState:UIControlStateNormal];
        [self setImage:_faceTwo_high forState:UIControlStateHighlighted];
    }
    if ([_delegate respondsToSelector:@selector(doubleFaceButtonImageDidChanged)]) {
        [_delegate doubleFaceButtonImageDidChanged];
    }
    else{
        if ([_delegate isKindOfClass:[UIView class]]) {
            [(UIView *)_delegate setNeedsLayout];
        }
    }
}
-(void)clearImg{
    [self setImage:nil forState:UIControlStateNormal];
    [self setImage:nil forState:UIControlStateHighlighted];

}
-(void)clearBacgroundImg{
    [self setBackgroundImage:nil forState:UIControlStateNormal];
    [self setBackgroundImage:nil forState:UIControlStateHighlighted];
}
+(DoubleFaceButton *)buttonWithType:(UIButtonType)buttonType{
    DoubleFaceButton *btn =[super buttonWithType:UIButtonTypeCustom];
    return btn;
}



@end
